package com.alnex.activiti;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.junit.Test;

import com.alnex.test04.Holiday;

/**
 * 处理当前用户的任务
 * 背后操作的表：
 *   act_hi_actinst
     act_hi_identitylink
     act_hi_taskinst
     act_ru_identitylink
     act_ru_task
 */
public class ActivitiCompleteTask {

    //查询当前用户wangwu的任务并处理掉
    public static void main(String[] args) {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2.得到TaskService对象
        TaskService taskService = processEngine.getTaskService();

        //3.查询当前用户的任务
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("holiday")
                .taskAssignee("wangwu")
                .singleResult();

        //4.处理任务,结合当前用户任务列表的查询操作的话,任务ID:task.getId()
        taskService.complete(task.getId());

        //5.输出任务的id
        System.out.println(task.getId());

        System.out.println("任务完成");

    }


    //lishi完成自己的任务
    @Test
    public void c1() {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2.得到TaskService对象
        TaskService taskService = processEngine.getTaskService();

        //3.处理任务,结合当前用户任务列表的查询操作的话,任务ID:5002
        try {
            taskService.complete("12502");
            System.out.println("任务完成");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("任务未完成");
        }
    }


    //zhangsan完成自己的任务
    @Test
    public void c2() {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2.得到TaskService对象
        TaskService taskService = processEngine.getTaskService();

        Holiday holiday = new Holiday();
        holiday.setNum(2f);
        //参数设置
        Map<String,Object> variables = new HashMap<>();
        variables.put("num",holiday.getNum());

        //3.处理任务,结合当前用户任务列表的查询操作的话,任务ID:2505

        try {
            taskService.complete("2508",variables);
            System.out.println("任务完成");

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("任务未完成");

        }
    }
}
